﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Dapper_Core.BLL.Impl
{
    public class SqlRepository
    {
        /// <summary>
        /// 根据用户获取侧边菜单资源
        /// </summary>
        public static string GetSideMenusByUser = @"select MenuName as text,MenuCode as id,ParentCode,PageUrl as url,Icon as icon from
                                                    (select distinct Sys_Role_Menu.MenuId from (select RoleCode from Sys_User_Role where UserCode=@userCode)t1
                                                    inner join Sys_Role_Menu on t1.RoleCode=Sys_Role_Menu.RoleCode)t2
                                                    inner join Sys_Menu on t2.MenuId=Sys_Menu.MenuId and Sys_Menu.MenuType=100100 and Sys_Menu.IsActive=1";

        /// <summary>
        /// 根据角色获取侧边菜单资源
        /// </summary>
        public static string GetSideMenusByRole = @"select MenuName as text,MenuCode as id,ParentCode,PageUrl as url,Icon as icon from
                                                    (select distinct Sys_Role_Menu.MenuId from Sys_Role_Menu where RoleCode=@roleCode)t1
                                                    inner join Sys_Menu on t1.MenuId=Sys_Menu.MenuId and Sys_Menu.MenuType=100100 and Sys_Menu.IsActive=1";

        /// <summary>
        /// 获取所有的菜单资源
        /// </summary>
        public static string GetExtendMenuList = @"select MenuId, MenuName, MenuCode, MenuType,isnull(DictName,'未知') as MenuTypeName, Sys_Menu.ParentCode, PageUrl, Icon, Sys_Menu.OrderNo,Sys_Menu.IsActive, Sys_Menu.IsDeleted,Sys_Menu.Creator,Sys_Menu.CreateDate,Sys_Menu.Remarks from Sys_Menu
                                                   left join Sys_Dictionary on Sys_Menu.MenuType=Sys_Dictionary.DictCode
                                                   where MenuCode != 0 and (MenuName like @search or MenuCode like @search) and Sys_Menu.IsDeleted=0
                                                   order by MenuCode asc";

        /// <summary>
        /// 将普通字符串转换成参数化like查询条件（左右加%，对字符串中的%进行特殊处理）
        /// </summary>
        /// <param name="condition">普通字符串</param>
        /// <returns></returns>
        public static string GetLikeCondition(string condition)
        {
            if (string.IsNullOrEmpty(condition)) return "%";
            return "%" + condition.Replace("%", "[%]") + "%";
        }
    }
}
